<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2019 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
declare (strict_types=1);

namespace app;

use think\App;
use think\exception\ValidateException;
use think\Validate;
use mikkle\tp_tools\ApiCode;
use think\Response;

/**
 * 控制器基础类
 */
abstract class BaseController
{
    /**
     * Request实例
     * @var \think\Request
     */
    protected $request;

    /**
     * 应用实例
     * @var \think\App
     */
    protected $app;

    /**
     * 是否批量验证
     * @var bool
     */
    protected $batchValidate = false;

    /**
     * 控制器中间件
     * @var array
     */
    protected $middleware = [];

    /**
     * 构造方法
     * @access public
     *
     * @param App $app 应用对象
     */
    public function __construct(App $app)
    {
        $this->app = $app;
        $this->request = $this->app->request;

        // 控制器初始化
        $this->initialize();
    }

    // 初始化
    protected function initialize()
    {
    }

    /**
     * 验证数据
     * @access protected
     *
     * @param array $data 数据
     * @param string|array $validate 验证器名或者验证规则数组
     * @param array $message 提示信息
     * @param bool $batch 是否批量验证
     *
     * @return array|string|true
     * @throws ValidateException
     */
    protected function validate(array $data, $validate, array $message = [], bool $batch = null)
    {
        if (is_array($validate)) {
            $v = new Validate();
            $v->rule($validate);
        } else {
            if (strpos($validate, '.')) {
                // 支持场景
                list($validate, $scene) = explode('.', $validate);
            }
            $class = false !== strpos($validate, '\\') ? $validate : $this->app->parseClass('validate', $validate);
            $v = new $class();
            if (!empty($scene)) {
                $v->scene($scene);
            }
        }

        $v->message($message);

        // 是否批量验证
        if ($batch || $this->batchValidate) {
            $v->batch(true);
        }

        return $v->failException(true)->check($data);
    }

    /**
     * 仅仅是返回一个固定格式的数组，控制器里面需要json函数返回给页面
     *
     * @param string $code
     * @param array $data
     * @param string $msg
     * @param array $append
     *
     * @return array
     */
    public function returnCode($code = '', $data = [], $msg = '', array $append = [])
    {
        return ApiCode::jsonCode($code, $data, $msg, $append);
    }

    /**
     * 直接返回json对象给前端，控制器直接return 这个结果就可以
     *
     * @param string $code
     * @param array $data
     * @param string $msg
     * @param array $append
     *
     * @return Response
     */
    public function returnJson($code = '', $msg = '', $data = [], array $append = [])
    {
        $rdata = ApiCode::jsonCode($code, $data, $msg, $append);
        return Response::create($rdata, 'json', 200, [], []);
    }

    /**
     * 需要建立模型，模型分页返回的结果放到data里面，直接return结果给前端
     *
     * @param string $code
     * @param string $msg
     * @param array $data
     * @param array $append
     *
     * @return Response
     */
    public function returnPageJson($code = '', $msg = '', $data = [], array $append = [])
    {
        $params = $this->request->param();
        $returnData['results'] = $data['data'];
        $returnData['totalPage'] = $data['last_page']; //总页数
        $returnData['pageSize'] = $data['per_page'];
        $returnData['totalRecord'] = $data['total'];
        $returnData['pageNo'] = $data['current_page'];
        if (!empty($data['header'])) {

            $returnData['header'] = $data['header'];
        }
        $returnData['params'] = $params;
        $rdata = ApiCode::jsonCode($code, $returnData, $msg, $append);
        return Response::create($rdata, 'json', 200, [], []);
    }

    /**
     * 自定义分页返回给前端数据，控制器需要用json函数返回
     *
     * @param string $code 返回编码
     * @param string $msg 返回的信息
     * @param $total    总页数
     * @param $paginate 每页数量
     * @param $data 当前数据
     * @param array $append 其他数据
     *
     * @return array
     */
    public function returnPageCode($code = '', $msg = '', $total, $paginate, $list, array $append = [])
    {
        $params = $this->request->param();
        $data['pageNo'] = !empty($params['page']) ? $params['page'] : 1;
        unset($params['page']);
        $data['totalPage'] = ceil($total / $paginate); //总页数
        $data['pageSize'] = $paginate;
        $data['totalRecord'] = $total;
        $data['params'] = $params;
        $data['results'] = $list;

        return ApiCode::jsonCode($code, $data, $msg, $append);
    }


    public function _empty()
    {
        return $this->returnJson(10010, '不存在方法');
    }

}
